package com.test.demo.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.pd.mvc.base.BaseController;
import com.pd.mvc.base.JsonResult;
import com.pd.mvc.page.PageResult;
import com.pd.mvc.utils.ServletUtils;
import com.test.annotation.OLog;
import com.test.constant.FileConstant;
import com.test.constant.LogConstant;
import com.test.security.entity.LoginUser;
import com.test.security.entity.SysConfig;
import com.test.security.mapper.SysConfigMapper;
import com.test.security.service.TokenService;
import com.test.utils.file.FileUploadUtils;

@RestController
public class SysTestController extends BaseController {
	@Autowired
	private SysConfigMapper sysConfigMapper;
	@Autowired
	private TokenService tokenService;

	@PostMapping("/avatar")
	public JsonResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException {
		if (!file.isEmpty()) {
			LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
			String avatar = FileUploadUtils.upload(FileConstant.RESOURCE_AVATAR, file);
			return JsonResult.success("上传成功！", avatar);
		}
		return JsonResult.fail500("上传图片异常，请联系管理员");
	}

	@GetMapping("test")
	@OLog(title = "test查询", businessType = LogConstant.OPERATION_OTHER)
	@PreAuthorize("hasAnyRole('ROLE_admin')")
	public JsonResult<PageResult> test(SysConfig entity) {
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		log.debug("{}========", loginUser);
		startPage();
		List<SysConfig> list = null;
		SysConfig config = new SysConfig();
		config.setConfigKey("a");
		list = sysConfigMapper.selectConfigList(config);
		// list = sysConfigService.selectConfigList(entity);
		return JsonResult.success("查询成功！", getPageData(list));
	}

}
